import { isObject } from "../shared/index";
import { track, trigger } from "./effect";
import { reactive } from "./reactive";

export function ref(value) { // 这里的value是一个基本类型的值
  return createRef(value)
}

function createRef(rawValue,shallow = false) {
  return new RefImpl(rawValue,shallow)
}

const convert = (val) => isObject(val) ? reactive(val) : val

class RefImpl {
  private _value;
  public readonly __v_isRef = true;
  constructor(private _rawValue,public readonly _shallow) {
    this._value = _shallow ? _rawValue : convert(_rawValue)
  }

  // 类的属性访问器
  get value () {
    track(this,'value')
    return this._value
  }

  set value(newValue) {
    this._rawValue = newValue;
    this._value = this._shallow ? newValue : convert(newValue);
    trigger(this,'set', 'value', this._rawValue)
  }
}